#include <iostream>
#include <vector>

int dfs(std::vector<std::vector<bool>> visited,
        std::vector<std::vector<int>> matx, int i, int j, int &cnt)
{
    if (i < 0 || i >= matx.size() || j < 0 || j >= matx[0].size())
        return 0;

    // std::cout << i << " " << j << std::endl;
    visited[i][j] = true;
    if (i + 1 < matx.size() && !visited[i + 1][j] && abs(matx[i][j] - matx[i + 1][j]) <= 1)
    {
        cnt++;
        dfs(visited, matx, i + 1, j, cnt);
    }
    if (i - 1 >= 0 && !visited[i - 1][j] && abs(matx[i][j] - matx[i - 1][j]) <= 1)
    {
        cnt++;
        dfs(visited, matx, i - 1, j, cnt);
    }
    if (j + 1 < matx[0].size() && !visited[i][j + 1] && abs(matx[i][j] - matx[i][j + 1]) <= 1)
    {
        cnt++;
        dfs(visited, matx, i, j + 1, cnt);
    }
    if (j - 1 >= 0 && !visited[i][j - 1] && abs(matx[i][j] - matx[i][j - 1]) <= 1)
    {
        cnt++;
        dfs(visited, matx, i, j - 1, cnt);
    }

    return 0;
}

int main()
{
    int M, N;
    std::cin >> M >> N;

    std::vector<std::vector<bool>> visited(M, std::vector<bool>(N, false));
    std::vector<std::vector<int>> matx(M, std::vector<int>(N, 0));

    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
            std::cin >> matx[i][j];
    }

    int maxv = 0;
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (!visited[i][j])
            {
                // std::cout << matx[i][j] << std::endl;
                int cnt = 1;
                dfs(visited, matx, i, j, cnt);
                if (maxv < cnt)
                    maxv = cnt;
            }
        }
    }

    std::cout << maxv << std::endl;

    return 0;
}